/*
B2	forward
B3	back
B4	+
A5	-
A1	AC
A4	AC
A3	lamp
A6	dias not inserted
*/

#define DIAS_DETECTOR_PIN		PORTBbits.RB4
#define ZERO_CROSS_DETECTOR_PIN	PORTBbits.RB0
#define LAMP_PIN				PORTBbits.RB3
#define FORWARD_PIN				PORTBbits.RB2
#define BACKWARD_PIN			PORTBbits.RB1
#define LED_PIN					PORTBbits.RB5

#define TX_PIN					PORTCbits.RC6
#define RX_PIN					PORTCbits.RC7

#define TRIS_ZERO_CROSS_DETECTOR_PIN	TRISBbits.TRISB0
#define TRIS_BACKWARD_PIN				TRISBbits.TRISB1
#define TRIS_FORWARD_PIN				TRISBbits.TRISB2
#define TRIS_LAMP_PIN					TRISBbits.TRISB3
#define TRIS_DIAS_DETECTOR_PIN			TRISBbits.TRISB4
#define TRIS_LED						TRISBbits.TRISB5

#define TRIS_AD_0						TRISAbits.TRISA0
#define TRIS_AD_1						TRISAbits.TRISA1

#define TRIS_TX_PIN						TRISCbits.TRISC6
#define TRIS_RX_PIN						TRISCbits.TRISC7

#define LAMP_ON_TIME			100
#define LAMP_OFF_TIME			100
#define NEXT_DIAS_TIME			800				// 0.8 seconds
#define DIAS_CHANGE_TIME		1000			// 1 seconds
#define BOOT_WAIT_TIME			10000
#define BOOT_POST_WAIT_TIME		10000

#define MAX_DIMMER_LEVEL		100 * (255/100)	// 50%

#define TIMER0_RELOAD 0xFF5D	// 39 us @ 20MHz
#define TIMER1_RELOAD 0xFD91	// 1 ms @ 20MHz
#define TIMER2_RELOAD 0x7C		// 1 ms @ 20MHz
#define TIMER3_RELOAD 0x0

static __code char __at(__CONFIG1L) conf1L = _PLLDIV_DIVIDE_BY_5__20MHZ_INPUT__1L &
					     _CPUDIV__OSC1_OSC2_SRC___1__96MHZ_PLL_SRC___2__1L &
					     _USBPLL_CLOCK_SRC_FROM_OSC1_OSC2_1L;

static __code char __at(__CONFIG1H) conf1H = _OSC_HS__USB_HS_1H &
					     _FCMEN_OFF_1H &
					     _IESO_OFF_1H;

static __code char __at(__CONFIG2L) conf2L = _PUT_OFF_2L &
					     _BODEN_ON_2L &
					     _BODENV_2_7V_2L &
					     _VREGEN_ON_2L;

static __code char __at(__CONFIG2H) conf2H = _WDT_DISABLED_CONTROLLED_2H;

static __code char __at(__CONFIG3H) conf3H = _MCLRE_MCLR_ON_RE3_OFF_3H &
					     _LPT1OSC_OFF_3H &
					     _PBADEN_PORTB_4_0__CONFIGURED_AS_DIGITAL_I_O_ON_RESET_3H &
					     _CCP2MUX_RB3_3H;

static __code char __at(__CONFIG4L) conf4L = _STVR_ON_4L &
					     _LVP_OFF_4L &
					     _ENICPORT_OFF_4L &
					     _ENHCPU_OFF_4L;

static __code char __at(__CONFIG5L) conf5L = _CP_0_OFF_5L & 
					     _CP_1_OFF_5L &
					     _CP_2_OFF_5L &
					     _CP_3_OFF_5L;


static __code char __at(__CONFIG5H) conf5H = _CPB_OFF_5H & _CPD_OFF_5H;

static __code char __at(__CONFIG6L) conf6L = _WRT_0_OFF_6L &
					     _WRT_1_OFF_6L &
					     _WRT_2_OFF_6L &
					     _WRT_3_OFF_6L;

static __code char __at(__CONFIG6H) conf6H = _WRTB_OFF_6H &
					     _WRTC_OFF_6H &
					     _WRTD_OFF_6H;

static __code char __at(__CONFIG7L) conf7L = _EBTR_0_OFF_7L &
					     _EBTR_1_OFF_7L &
					     _EBTR_2_OFF_7L &
					     _EBTR_3_OFF_7L;

static __code char __at(__CONFIG7H) conf7H = _EBTRB_OFF_7H;

typedef enum direction_t{
	FORWARD,
	BACKWARD
} direction_t;

typedef enum dimmer_direction_t {
	UP,
	DOWN
} dimmer_direction_t;

typedef enum command_t {
	CMD_SYNC = 3,
	CMD_FORWARD = 6,
	CMD_BACKWARD = 2,
	CMD_FADE_IN = 1,
	CMD_FADE_OUT = 7
} command_t;

void sleep_ms(unsigned long ms);
void fade_in(void);
void fade_out(void);
void forward_dias(void);
void backward_dias(void);
void sync_dias(unsigned char pos);
static void phase_control(void) __interrupt 1;
static void timer_control(void) __interrupt 2;
void my_usart_open();
void wait_for_command();

void led_debug();

unsigned char fifo_in_use();
unsigned char fifo_put(command_t  c);
unsigned char fifo_get(command_t *c);
unsigned char fifo_snoop(command_t *c, unsigned char pos);
unsigned char fifo_get_optimized(command_t *c);

unsigned char read_EEPROM(unsigned char address);
void write_EEPROM(unsigned char address, unsigned char d);
